2
2
.
.
6
6
.
.
3
3
P
P
o
o
s
s
t
t
g
g
r
r
e
e
S
S
Q
Q
L
L
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial show how to use Repository to work with PostgreSQL Database.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @RequestMapping (includes Tomcat Server)
SQL
Spring Data JPA
Enables @Entity and @Id
SQL
PostgreSQL Database
Enables Hibernate to work with PostgreSQL DB
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_db_postgresql (add Spring Boot Starters from the table)
Edit File: application.properties (enter DB connection parameters)
Create Package: entities (inside main package)
– Create Java Class: PersonEntity.java (inside package entities)
Create Package: repositories (inside main package)
– Create Java Interface: PersonRepository.java (inside package repositories)
Create Package: controllers (inside main package)
– Create Java Class: MyController.java (inside package controllers)
application.properties
# POSTGRESQL DATABASE
spring.datasource.url = jdbc:postgresql://localhost:5432/postgres
spring.datasource.username = postgres
spring.datasource.password = letmein
spring.datasource.driver-class-name = org.postgresql.Driver
# JPA / HIBERNATE
spring.jpa.hibernate.ddl-auto = create-drop
MyController
http://localhost:8080/AddPerson
addPerson()
PersonEntity
PostgreSQL
PersonEntity.java
package com.ivoronline.springboot_db_postgresql.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class PersonEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
public String name;
public Integer age;
}
PersonRepository.java
package com.ivoronline.springboot_db_postgresql.respositories;
import com.ivoronline.springboot_db_postgresql.entities.PersonEntity;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<PersonEntity, Integer> { }
MyController.java
package com.ivoronline.springboot_db_postgresql.controllers;
import com.ivoronline.springboot_db_postgresql.entities.PersonEntity;
import com.ivoronline.springboot_db_postgresql.respositories.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@Autowired PersonRepository personRepository;
@ResponseBody
@RequestMapping("/AddPerson")
public String addPerson() {
//CREATE PERSON ENTITY
PersonEntity personEntity = new PersonEntity();
personEntity.name = "John";
personEntity.age = 20;
//STORE PERSON ENTITY
personRepository.save(personEntity);
//RETURN SOMETHING TO BROWSER
return "Person added to DB";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/AddPerson
pgAdmin4 (http://127.0.0.1:50043/browser)
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>